package euler.p001_050;

import java.util.HashSet;
import java.util.Set;

import euler.MainEuler;
import euler.helper.NaturalHelper;

public class Euler035 extends MainEuler {

    /*
        The number, 197, is called a circular prime
        because all rotations of the digits: 197, 971, and 719, are themselves prime.

        There are thirteen such primes below 100:
        2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.

        How many circular primes are there below one million?
     */
    public String resolve(int limite) {

        Set<Integer> set = new HashSet<Integer>();

        for (int i = 2; i < limite; i++) {
            if (!set.contains(i) && primeHelper.isPrime(i)) {
                int[] r = rotations(i);

                boolean allPrime = true;
                for (int j = 0; allPrime && (j < r.length); j++) {
                    allPrime = primeHelper.isPrime(r[j]);
                }

                if (allPrime) {
                    for (int j = 0; j < r.length; j++) {
                        set.add(r[j]);
                    }
                }
            }
        }

        return String.valueOf(set.size());
    }

    private int[] rotations(int n) {

        int base = 10;
        int d = NaturalHelper.cantidadDigitos(n, base);
        int[] r  = new int[d];

        r[0] = n;
        for (int i = 1; i < r.length; i++) {
            r[i] = (r[i - 1] / base) + (r[i - 1] % base) * (int)Math.pow(base,d - 1);
        }

        return r;
    }

}
